define(['jquery', 'bootstrap', 'backend', 'table', 'form','bootstrap-table-jumpto'], function ($, undefined, Backend, Table, Form,showJumpto) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        recyclebin: function () {
            console.log('afeawf');
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                var toAjax = function (ids, index,url,layero) {
                    var options = table1.bootstrapTable('getOptions');
                    var search = options.queryParams({});
                    Fast.api.ajax({
                        url: url,
                        data: {
                            ids: ids,
                            filter: search.filter,
                            op:search.op,
                            search: options.searchText,
                            pay_type: $("input[name=pay_type]:checked", layero).val(),
                            remark: $("textarea[name=remark]", layero).val(),
                        }
                    }, function (ret) {
                        Layer.close(index);
                        $(".btn-refresh").trigger("click");
                    });
                };
                $(document).on("click", ".my-check-money", function () {
                    var ids = Table.api.selectedids(table1);  //所有选中的项
                    var page = table1.bootstrapTable('getData');  //当前页面
                    var all = table1.bootstrapTable('getOptions').totalRows; //所有行
                    var url = 'zbtaskcompany/money/check';
                    Layer.confirm("<form id='check-piliang' class='form-horizontal'  action='' method='post'>" +
                        "<div class='form-group'>" +
                        "        <label class='control-label col-xs-12 col-sm-4'>状态:</label>" +
                        "        <div class='col-xs-12 col-sm-8'>" +
                        "            <div class='radio'>" +
                        "                <label for='type-1'><input id='type-1' name='pay_type' type='radio' value='1' checked> 通过</label>" +
                        "                <label for='type-2'><input id='type-2' name='pay_type' type='radio' value='2'> 拒绝</label>" +
                        "            </div>" +
                        "        </div>" +
                        "    </div>"+
                        "<textarea class='form-control' rows='3' name='remark'></textarea>" +

                        "</form>",
                        {
                            title: '审核数据',
                            btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                            yes: function (index, layero) {
                                toAjax(ids, index,url,layero);
                                return false;
                            },
                            btn2: function (index, layero) {
                                var ids = [];
                                $.each(page, function (i, j) {
                                    ids.push(j.id);
                                });
                                toAjax(ids, index, url,layero);
                                return false;
                            },
                            btn3: function (index, layero) {
                                toAjax('all', index, url,layero);
                                return false;
                            }
                        });
                });
                function getTaskId() {
                    var options = table1.bootstrapTable('getOptions');
                    var search = options.queryParams({});
                    var filter = JSON.parse(search.filter);

                    if (typeof filter.task_id !== "undefined") {
                        return filter.task_id;
                    }

                    return 0;
                }
                $('#toolbar1').on("click", "#btn-many-transfer", function () {
                    var ids = Table.api.selectedids(table1);  //所有选中的项
                    var page = table1.bootstrapTable('getData');  //当前页面
                    var all = table1.bootstrapTable('getOptions').totalRows; //所有行
                    getTaskId();
                    var curPage = [];
                    $.each(page, function (i, j) {
                        curPage.push(j.id);
                    });

                    var select = ids.join(",");
                    curPage = curPage.join(",");
                    var task_id = getTaskId();
                    var url =  'zbtaskcompany/money/transpage?select=' + select + '&curPage=' + curPage + '&task_id=' + task_id;
                    Fast.api.open(url, '批量打款');
                });
                $.fn.bootstrapTable.locales[Table.defaults.locale].formatSearch = function(){return "ID/姓名/身份证/任务名称";};
                //在普通搜索提交搜索前
                table1.on('common-search.bs.table', function (event, table, query) {
                    //这里可以获取到普通搜索表单中字段的查询条件
                    $('#success_money').html('加载中...');
                });
                // 初始化表格
                table1.bootstrapTable({
                    url: 'zbtaskcompany/money/index' + location.search,
                    extend: {
                        index_url: 'zbtaskcompany/money/index' + location.search,
                        add_url: 'zbtaskcompany/money/add',
                        edit_url: 'zbtaskcompany/money/edit',
                        del_url: 'zbtaskcompany/money/del',
                        multi_url: 'zbtaskcompany/money/multi',
                        table: 'zbtask_money',
                    },
                    toolbar: '#toolbar1',
                    pk: 'id',
                    sortName: 'id',
                    showJumpto: true,
                    searchFormVisible: true,
                    showExport: false,
                    onLoadSuccess: function (data) {
                        $('#success_money').html(data.statistics);
                    },
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                            {field: 'user.real_name', title: __('User.real_name')},
                            {field: 'user.identity_card', title: '身份证', operate: 'like'},
                            {field: 'pay_type', visible: false,title: __('Pay_type'), searchList: {"0":__('Pay_type 0'),"1":__('Pay_type 1'),"2":__('Pay_type 2')}, formatter: Table.api.formatter.normal},
                            {field: 'bank_card', visible: false,title: __('Bank_card'),operate: false},
                            {field: 'zbtask.name', title: __('Zbtask.name'), operate: false,formatter: function(value,row){
                                return '<a href="zbtask/zbtask/detail/ids/'+row.task_id+'" data-area=\'["90%", "90%"]\' class="btn btn-dialog" title="任务详情" data-button-index="0"><i class="fa fa-chain">'+value+'</i></a>';
                            }},
                            {field: 'task_id', title: __('Task_id'),visible:false, addClass: "selectpage", extend: "data-source='zbtask/zbtask/index'  data-field='name' data-order-by='id desc'"},
                            {field: 'money', title: __('Money'), operate:false},
                            {field: 'get_money', title:'实发金额', operate:false},
                            {field: 'ca_money', title: __('Ca_money'),operate: false, visible: false},
                            {field: 'item', title: __('Item'), visible: false, operate: false},
                            {field: 'tax_money', title: __('Tax_money'), operate:false},
                            {field: 'createtime', title: __('Createtime'), cellStyle:Controller.api.css.time,operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                            {field: 'status', title: __('Status'), operate: false, visible: false,searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                            {field: 'transfer_type', title: __('Transfer_type'),searchList: {"0":'未打款',"1":'打款中',"2":'打款成功',"3":'打款失败'},formatter: function (value, row, index) {
                                return '<a href="zbtask/money/transferLog/ids/'+row.id+'" data-area=\'["60%", "60%"]\' class="btn btn-dialog" title="打款日志" data-button-index="0" style="color: red"><i class="fa fa-chain">'+row.transfer_type_text+'</i></a>';
                            }},
                            {field: 'pay_time', title: __('Pay_time'), operate:false, visible: false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                            {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},

                        ]
                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                var table2 = $("#table2");
                var toAjax = function (ids, index,url) {
                    var options = table2.bootstrapTable('getOptions');
                    var search = options.queryParams({});
                    Fast.api.ajax({
                        url: url,
                        data: {
                            ids: ids,
                            filter: search.filter,
                            op:search.op,
                            search: options.searchText
                        }
                    }, function (ret) {
                        Layer.close(index);
                        $(".btn-refresh").trigger("click");
                    });
                };
                $(document).on("click", ".btn-selected-file", function () {
                    var ids = Table.api.selectedids(table2);  //所有选中的项
                    var page = table2.bootstrapTable('getData');  //当前页面
                    var all = table2.bootstrapTable('getOptions').totalRows; //所有行
                    var url = 'zbtask/import/sureSubmit';
                    Layer.confirm("确认提交数据到正式众包费吗？",
                        {
                            title: '确认提交到正式数据吗？',
                            btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                            yes: function (index, layero) {
                                toAjax(ids, index,url);
                                return false;
                            },
                            btn2: function (index, layero) {
                                var ids = [];
                                $.each(page, function (i, j) {
                                    ids.push(j.id);
                                });
                                toAjax(ids, index, url);
                                return false;
                            },
                            btn3: function (index, layero) {
                                toAjax('all', index, url);
                                return false;
                            }
                        });
                });
                $.fn.bootstrapTable.locales[Table.defaults.locale].formatSearch = function(){return "姓名/身份证";};
                // 初始化表格
                table2.bootstrapTable({
                    url: 'zbtask/import/index' + location.search,
                    extend: {
                        index_url: 'zbtask/import/index' + location.search,
                        add_url: 'zbtask/import/add',
                        edit_url: 'zbtask/import/edit',
                        del_url: 'zbtask/import/del',
                        multi_url: 'zbtask/import/multi',
                        table: 'zbtask_import',
                    },
                    toolbar: '#toolbar2',
                    pk: 'id',
                    sortName: 'id',
                    showJumpto: true,
                    searchFormVisible: true,
                    showExport: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'), operate:false},
                            {field: 'zbtask.name', title: __('Zbtask.name'),operate:false,cellStyle:Controller.api.css.name},
                            {field: 'real_name', title: __('Real_name')},
                            {field: 'identity_card', title: __('Identity_card')},
                            {field: 'bank_card', title: __('Bank_card'),operate:false},
                            {field: 'task_id', title: __('Task_id'),visible:false, addClass: "selectpage", extend: "data-source='zbtask/zbtask/index'  data-field='name' data-order-by='id desc'"},
                            {field: 'mobile', title: __('Mobile'),operate:false},
                            {field: 'money', title: __('Money'),operate:false},
                            {field: 'type', title: '账户类别', searchList: {"0":"银行卡","1":"支付宝","2":"微信"}, formatter: Table.api.formatter.normal},
                            {field: 'status', title: '格式是否正确', searchList: {"0":'待验证',"1":'正确', "2":'错误'}, formatter: Table.api.formatter.status},
                            {field: 'money_type', title: "是否实发", searchList: {"1":"应发","2":"实发"}, formatter: Table.api.formatter.normal},
                            {field: 'is_import', title: "是否导入", searchList: {"0":"未导入","1":"导入"}, formatter: Table.api.formatter.normal},
                            {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ],

                });

                // 为表格绑定事件
                Table.api.bindevent(table2);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $("input[name='row[money_type]']").click(function () {
                var val = $(this).val();
                var money = $(this).data('money');
                $('#c-cal_money').val(money);
            });
            $('#reimport').click(function () {
                var money_type = $("input[name='row[money_type]']:checked").val();
                var money = $('#c-cal_money').val();
                var ids =$("input[name='ids']").val();
                Fast.api.ajax({
                    url: 'zbtask/money/reImport',
                    data: {
                        money_type: money_type,
                        money: money,
                        ids: ids
                    }
                }, function () {
                    location.reload();
                });
            });
        },
        transpage: function() {
            $('input[name=pay_type]').click(function () {
                var val = $(this).val();
                if (val == 0) {
                    $('.order_sn_wrap').show();
                } else {
                    $('.order_sn_wrap').hide();
                }
            });
            Form.api.bindevent($("form[role=form]"), function(data, ret){
                //给表单绑定新的回调函数 接收 控制器 success(msg,url,data)或者error(msg,url,data)
                Fast.api.close(data);//在这里
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            css: {
                name: function () {
                    return {
                        css: {
                            'max-width': '350px',
                            'word-wrap': 'break-word',
                            'word-break': 'normal'
                        }
                    };
                },
                time: function () {
                    return {
                        css: {
                            'width': '150px',
                            'word-wrap': 'break-word',
                            'word-break': 'normal'
                        }
                    };
                },
                remark: function () {
                    return {
                        css: {
                            'width': '200px',
                            'word-wrap': 'break-word',
                            'word-break': 'normal'
                        }
                    };
                }
            }
        }
    };
    return Controller;
});